package com.yuncheng.spcyApi.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.entity.SpcyLog;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.mapper.SpcyLogMapper;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;


import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.CurrentUserVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.HttpContextUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 审评查验日志记录
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcyLogServiceImpl extends BaseServiceImpl<SpcyLogMapper, SpcyLog> implements ISpcyLogService {

    @Resource
    private SpcyLogMapper spcyLogMapper;

    @Resource
    @Lazy
    private YcSysUserMapper ycSysUserMapper;

    @Async(value = "asyncSimpleTask")
    @Override
    public void addLogs(String nodeName, String operationContent, String pid, String operationType1, String operationType2) {

        if (StringUtils.isNotBlank(nodeName) && nodeName.equals(LogConstant.LOGS_JCYGL) && StringUtils.isNotBlank(pid)){
            YcSysUser user = ycSysUserMapper.selectById(pid);
            if (user != null) {
                operationType1 += "/账号【" + user.getUsername() + "】";
                operationType1 += "/姓名【" + user.getRealname() + "】";
            }
        }

        SpcyLog spcyLog = new SpcyLog();

        String operationIp = HttpContextUtils.getIpAddress(); // 获取ip地址
        spcyLog.setId(GetUuIdUtils.NotReplaceUuId());
        spcyLog.setFip(operationIp);
        spcyLog.setFcontent(operationContent); // 操作内容
        spcyLog.setFpid(pid); // 业务id
        spcyLog.setFhjmc(nodeName); // 环节名称

        String nowUser = "";
        String nowUserDept = "";
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            nowUser = currentUser.getName();
            nowUserDept = currentUser.getDeptName();
        }
        spcyLog.setFczr(nowUser);
        spcyLog.setFczbm(nowUserDept);
        spcyLog.setFtype1(operationType1);
        spcyLog.setFtype2(operationType2);
        spcyLog.setCreateTime(new Date());
        spcyLog.setUpdateTime(new Date());
        spcyLog.setCreateBy(nowUser);
        spcyLog.setUpdateBy(nowUser);

        spcyLogMapper.insert(spcyLog);
    }

    @Override
    public void addLogRs(String nodeName, String operationContent, String pid, String operationType1, String operationType2, String type3, String type4) {
        SpcyLog spcyLog = new SpcyLog();

//        String operationIp = HttpContextUtils.getIpAddress(); // 获取ip地址
        spcyLog.setId(GetUuIdUtils.NotReplaceUuId());
        spcyLog.setFip("127.0.0.1");
        spcyLog.setFcontent(operationContent); // 操作内容
        spcyLog.setFpid(pid); // 业务id
        spcyLog.setFhjmc(nodeName); // 环节名称

        String nowUser = "";
        String nowUserDept = "";
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser != null){
            nowUser = currentUser.getName();
            nowUserDept = currentUser.getDeptName();
        }
        spcyLog.setFczr(nowUser);
        spcyLog.setFczbm(nowUserDept);
        spcyLog.setFtype1(operationType1);
        spcyLog.setFtype2(operationType2);
        spcyLog.setCreateTime(new Date());
        spcyLog.setUpdateTime(new Date());
        spcyLog.setCreateBy(nowUser);
        spcyLog.setUpdateBy(nowUser);

        spcyLogMapper.insert(spcyLog);
    }

    @Override
    public List<SpcyLog> getLogList(String nodeName, String pid) {

        LambdaQueryWrapper<SpcyLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(nodeName), SpcyLog::getFhjmc, nodeName);
        queryWrapper.eq(StringUtils.isNotBlank(pid), SpcyLog::getFpid, pid);

        queryWrapper.orderByDesc(SpcyLog::getCreateTime);
        List<SpcyLog> list = spcyLogMapper.selectList(queryWrapper);

        return list;
    }
}
